package com.pool.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.pool.R;
import com.pool.activity.common.FullscreenActivity;
import com.pool.common.Record;
import com.pool.common.Shoot;
import com.pool.store.RecordStore;
import com.pool.store.ShootStore;
import com.pool.store.Store;

/**
 * @author claveld@gmail.com (David Clavel)
 */
public class TestUI extends FullscreenActivity {

  private static final int SELECT_SHOOT = 0;

  private static final int BT_DAVID = 0;
  private static final int BT_MATEUSZ = 1;
  private static final int BT_VIEW_LOCAL_STORE = 2;
  private static final int BT_SAVE = 3;
  private static final int BT_SYNC = 4;
  private static final int BT_EXIT = 5;
  
  private static final String[] PLAYERS = {"David", "Mateusz"};
  
  private final Store<Record> recordStore;
  private final Store<Shoot> shootStore;

  public TestUI() {
    this(new RecordStore(), new ShootStore());
  }

  public TestUI(Store<Record> recordStore, Store<Shoot> shootStore) {
    this.recordStore = recordStore;
    this.shootStore = shootStore;
  }
  
  final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case BT_DAVID:
        case BT_MATEUSZ:
          String player = PLAYERS[msg.what];
          Intent intent = SelectShoot.createIntentForPlayer(getApplicationContext(), player);
          startActivityForResult(intent, SELECT_SHOOT);
          break;
        case BT_VIEW_LOCAL_STORE:
          startActivity(new Intent(getApplicationContext(), ViewLocalStore.class));
          break;
        case BT_SAVE:
          startActivity(LoginAndRun.createFor(getApplicationContext(), Synchronize.class));
          break;
        case BT_SYNC:
          startActivity(LoginAndRun.createFor(getApplicationContext(), Synchronize.class));
          break;
        case BT_EXIT:
          setResult(RESULT_OK, getIntent());
          finish();
          break;
        default:
          break;
      }
    };
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_ui);

    View view = findViewById(R.id.test_ui_layout);
    registerButton(view, R.id.bt_david, BT_DAVID);
    registerButton(view, R.id.bt_mateusz, BT_MATEUSZ);
    registerButton(view, R.id.bt_view_local_store, BT_VIEW_LOCAL_STORE);
    registerButton(view, R.id.bt_save, BT_SAVE);
    registerButton(view, R.id.bt_sync, BT_SYNC);
    registerButton(view, R.id.bt_exit, BT_EXIT);
  }

  private void registerButton(View view, int id, final int message) {
    Button button = (Button) view.findViewById(id);
    button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        handler.sendEmptyMessage(message);
      }
    });
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SELECT_SHOOT) {
      if (resultCode == RESULT_OK) {
        String player = data.getExtras().getString(SelectShoot.PLAYER);
        String shootId = data.getExtras().getString("shoot");
        int remainingBalls = data.getExtras().getInt("balls");
        Shoot shoot = shootStore.get(shootId);
        saveShoot(player, shoot, remainingBalls);
      }
    }
  }

  private void saveShoot(String player, Shoot shoot, int remainingBalls) {
    recordStore.add(Record.of(player, shoot.id, remainingBalls));
    String notificationText = player + ": " + shoot.description + " (" + shoot.bonus + " pts)";
    if (shoot.isVictory()) {
      String plural = "";
      if (remainingBalls > 1) { plural = "s"; }
        notificationText += "\n" + remainingBalls + " ball" + plural + " remaining";
    }
    Toast.makeText(this, notificationText, Toast.LENGTH_SHORT).show();
  }
}
